home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MYIO.ZIP / MYIO.H < prev    next >
C/C++ Source or Header  |  1995-11-02  |  3KB  |  99 lines

  1. // Myio.h
  2. // Specialised I/O class to demonstrate use of C++ iostream
  3. // facilities in a customised environment
  4. // Written by David L Nugent, June 1993.
  5. //
  6.  
  7. # if !defined(_Myio_h)
  8. # define _Myio_h 1
  9.  
  10.     // Forward declare classes
  11.  
  12. class Myio;
  13. class Mystreambuf;
  14. class Mystreambase;
  15. class Mystream;
  16.  
  17.     // Forward declare iostream classes
  18.  
  19. class iostream;
  20.  
  21.     //
  22.     // class Myio
  23.     // This is a simplistic class which simply fields
  24.     // input and output to a simulated stream device.
  25.     //
  26.     // In fact, it doesn't really do much at all other
  27.     // than read input from and send output to a
  28.     // circular queue, as though talking via a loopback
  29.     // pipe to itself.
  30.     //
  31.  
  32.  
  33. class Myio
  34. {
  35.     friend class Mystreambuf;
  36.  
  37.   public:
  38.  
  39.     Myio (int sz =2048);                    // sz = buffer size to allocate
  40.     virtual ~Myio (void);
  41.  
  42.     iostream & stream (void);               // Return (or create) stream
  43.  
  44.     int readok (void) const;                // Underflow check
  45.     int writeok (void) const;               // Overflow check
  46.     int gcount (void) const;                // Get # of chrs last read
  47.     int pcount (void) const;                // Get # of chrs last written
  48.     int count (void) const;                 // Get # of chrs in buffer
  49.     int size (void) const;                  // Get size of buffer
  50.     int dump (void) const;                  // Debugging - dumps buffer
  51.  
  52.     int write (char const * buf, int len);  // Put data into 'pipe'
  53.     int read (char * buf, int max);         // Read data from our 'pipe'
  54.  
  55.   private:
  56.  
  57.     enum
  58.     {
  59.         overflow    = 0x0001,   // Last write only partial
  60.         underflow   = 0x0002    // Last read only partial
  61.     };
  62.  
  63.     unsigned stat;              // Last read/write status
  64.     int _pcount;                // Last write count
  65.     int _gcount;                // Last read count
  66.     int bufsize;                // Size of our buffer
  67.     int bufchars;               // Chrs in buffer now
  68.     int bufidx;                 // Index into buffer (next put)
  69.     char * bufaddr;             // Pointer to buffer
  70.     Mystream * mystream;        // Stream associated with this object
  71.  
  72. };
  73.  
  74. inline int
  75. Myio::readok (void) const
  76.     {   return ((stat & Myio::underflow) == 0); }
  77.  
  78. inline int
  79. Myio::writeok (void) const
  80.     {   return ((stat & Myio::overflow) == 0);  }
  81.  
  82. inline int
  83. Myio::gcount (void) const
  84.     {   return _gcount;     }
  85.  
  86. inline int
  87. Myio::pcount (void) const
  88.     {   return _pcount;     }
  89.  
  90. inline int
  91. Myio::count (void) const
  92.     {   return bufchars;    }
  93.  
  94. inline int
  95. Myio::size (void) const
  96.     {   return bufsize;     }
  97.  
  98. # endif     // _Myio_h
  99.